package com.uphone.magicleafteacher.pro.activity;

import android.arch.lifecycle.LifecycleOwner;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.radish.baselibrary.Intent.IntentData;
import com.radish.framelibrary.view.glide.GlideUtils;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseGActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.CourseDetailBean;

import butterknife.BindView;

public class CoursedetailsActivity extends BaseGActivity {
    @BindView(R.id.img_cover)
    ImageView imgCover;
    @BindView(R.id.text_name)
    TextView textName;
    @BindView(R.id.text_starttime)
    TextView textStarttime;
    @BindView(R.id.text_endtime)
    TextView textEndtime;
    @BindView(R.id.text_mone)
    TextView textMone;
    @BindView(R.id.text_ji)
    TextView textJi;
    @BindView(R.id.text_content)
    TextView textContent;
    @BindView(R.id.text_code)
    TextView textCode;
    @BindView(R.id.other_courses_type)
    TextView other_courses_type;
    @IntentData
    private int id;

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_course_details;
    }

    @Override
    protected void initView() {
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.kaola);
        imgCover.setImageDrawable(rectRoundBitmap(image));
    }

    @Override
    protected void initTitle() {
        initTitle("课程详情");
    }

    @Override
    protected void initData() {
        MyApp.apiService(ApiService.class)
                .otherClassDetail("" + id)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    showLoading();
                })
                .doFinally(() -> {
                    closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) this)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        CourseDetailBean bean = GsonUtils.getGson().fromJson(jsonObject, CourseDetailBean.class);
                        if (bean != null && bean.getDetail() != null) {
                            loadData(bean.getDetail());
                        }
                    }
                }, new FailureConsumer());

    }

    private void loadData(CourseDetailBean.DetailBean bean) {
        GlideUtils.getInstance().loadCornerImage(this, CommonUtil.getImageUrl(bean.getFacePic()), SizeUtils.dp2px(5), imgCover);
        textName.setText(CommonUtil.getStr(bean.getClassName()));
        textStarttime.setText(CommonUtil.getStr(bean.getStartTime()));
        textEndtime.setText(CommonUtil.getStr(bean.getEndTime()));
        textMone.setText(CommonUtil.getStr(bean.getMoney() + ""));
        textJi.setText(CommonUtil.getStr("获" + bean.getJifen() + "积分"));
        textContent.setText("简介：" + CommonUtil.getStr(bean.getClassDesc()));
        textCode.setText("老师端参与码：" + CommonUtil.getStr(bean.getTeacherCode()));
        other_courses_type.setText(bean.getLiveStatus() == 0 ? "未开始" : bean.getLiveStatus() == 1 ? "直播中" : "已结束");
        other_courses_type.setBackgroundResource(bean.getLiveStatus() == 0 ? R.drawable.item_other_curriculum_notstarted : bean.getLiveStatus() == 1 ? R.drawable.item_other_curriculum_begin : R.drawable.item_other_curriculum_end);

    }

    @Override
    protected void initListener() {

    }

    private RoundedBitmapDrawable rectRoundBitmap(Bitmap bitmap) {
        //创建RoundedBitmapDrawable对象
        RoundedBitmapDrawable roundImg = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
        //抗锯齿
        roundImg.setAntiAlias(true);
        //设置圆角半径
        roundImg.setCornerRadius(15);
        return roundImg;
    }

}
